"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 目录无权限	双机场景，主机和备机data目录中其他目录无权限，双机切换
Create At   : 2020/12/07
@zou_jialiang0505328126
Description :
    1.修改主机dn1目录中base权限为000
    2.备机switchover
    3.恢复主机dn1目录中base权限为700
    4.重启集群
    5.备机switchover
    6.修改备机(原主机)dn1目录中base权限000
    7.备机switchover
    8.恢复备机（原主机)dn1目录中base权限700
    9.重启集群
    10.备机(原主机)switchover
Expect      :
    1.无
    2.主备切换失败
    3.无
    4.重启成功
    5.主备切换成功
    6.无
    7.主备切换失败
    8.无
    9.重启成功
    10.主备切换成功
History     :
    modified by @wan005 at 2022/01/21:优化用例去除-R
"""

import unittest
from yat.test import macro
from yat.test import Node
from testcase.utils.CommonSH import *
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class ModifyAudit(unittest.TestCase):
    commonshpri = CommonSH('PrimaryDbUser')
    commonshsta1 = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case181.py 开始执行------------')
        self.dbPrimaryDbUser = Node(node='PrimaryDbUser')
        self.dbStandby1DbUser = Node(node='Standby1DbUser')
        self.dbPrimaryRoot = Node(node='PrimaryRoot')
        self.Constant = Constant()
        self.common = Common('PrimaryRoot')
        self.dbname = 'postgres'

    def test_basebackup(self):
        self.log.info('--------------1.修改主机dn1目录中base权限为000-------------------------------')
        result = self.dbPrimaryDbUser.sh(f"chmod 000 {macro.DB_INSTANCE_PATH}").result()
        self.log.info(result)

        self.log.info('----------------------2.备机switchover-------------------')
        result = self.commonshsta1.executeGsctl('switchover', self.Constant.SWITCHOVER_SUCCESS_MSG, )
        self.assertFalse(result)

        self.log.info('--------------3.恢复主机dn1目录中base权限为700-------------------------------')
        result = self.dbPrimaryDbUser.sh(f"chmod 700 {macro.DB_INSTANCE_PATH}").result()
        self.log.info(result)

        self.log.info('---------------------4.重启集群----------')
        result = self.commonshpri.startDbCluster()
        self.assertTrue(result)

        self.log.info('----------------------5.备机switchover-------------------')
        result = self.commonshsta1.executeGsctl('switchover', self.Constant.SWITCHOVER_SUCCESS_MSG, )
        self.assertTrue(result)
        result = self.commonshsta1.exec_refresh_conf()
        self.assertTrue(result)

        self.log.info('--------------6.修改备机(原主机)dn1目录中base权限000-------------------------------')
        result = self.dbStandby1DbUser.sh(f"chmod 000 {macro.DB_INSTANCE_PATH}").result()
        self.log.info(result)

        self.log.info('----------------------7.备机switchover-------------------')
        result = self.commonshsta1.executeGsctl('switchover', self.Constant.SWITCHOVER_SUCCESS_MSG, )
        self.assertFalse(result)

        self.log.info('--------------8.恢复备机（原主机)dn1目录中base权限700-------------------------------')
        result = self.dbStandby1DbUser.sh(f"chmod 700 {macro.DB_INSTANCE_PATH}").result()
        self.log.info(result)

        self.log.info('---------------------9.重启集群----------')
        result = self.commonshpri.startDbCluster()
        self.assertTrue(result)

        self.log.info('----------------------10.备机switchover-------------------')
        result = self.commonshpri.executeGsctl('switchover', self.Constant.SWITCHOVER_SUCCESS_MSG, )
        self.assertTrue(result)
        result = self.commonshpri.exec_refresh_conf()
        self.assertTrue(result)

    def tearDown(self):
        self.log.info('-------------------------环境清理------------------------')
        self.log.info('--------------恢复目录权限------------------------')
        result = self.dbStandby1DbUser.sh(f"chmod 700 {macro.DB_INSTANCE_PATH}").result()
        self.log.info(result)
        result = self.dbPrimaryDbUser.sh(f"chmod 700 {macro.DB_INSTANCE_PATH}").result()
        self.log.info(result)

        primary_hostname = self.dbPrimaryDbUser.sh("cat /etc/hostname").result()
        self.log.info(primary_hostname)
        self.log.info(self.commonshpri.getDbClusterStatus('detail'))
        if self.commonshpri.check_whether_need_switch(primary_hostname):
            self.log.info('执行swithover,还原主备')
            switch_msg = self.commonshpri.executeGsctl('switchover', self.Constant.SWITCH_SUCCESS_MSG)
            self.log.info(switch_msg)

            self.log.info('执行refresh')
            refresh_msg = self.commonshpri.exec_refresh_conf()
            self.log.info(refresh_msg)

        self.log.info('----------Opengauss_Reliability_Dbsys_Case181.py 执行结束-----------------------')
